package com.lzx.adx.report.support.strategy.channel;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzx.adx.report.annnotions.ProductType;
import com.lzx.adx.report.constant.ProductChannelConstant;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.util.UrlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 融创
 */
@Component
@Slf4j
@ProductType(ProductChannelConstant.RONG_CHANG)
public class RongChuangReportStrategy extends AbstractReport{
    @Override
    public String getReportFinalUrl(ReportDTO reportDTO, AccountDTO accountDTO) {


        JSONObject jsonObject = JSONObject.parseObject(accountDTO.getDspExtend());

        String channel = jsonObject.getString("channel");
        String packId = jsonObject.getString("packId");
        String time=String.valueOf(System.currentTimeMillis());
        String token=SecureUtil.md5(channel+accountDTO.getDspSalt()+time);

        Map<String,Object> params=new HashMap<>();
        params.put("requestId", reportDTO.getBizRequestId());
        params.put("imei", reportDTO.getImei());
        params.put("oaid",reportDTO.getOaid());
        params.put("idfa",reportDTO.getIdfa());
        params.put("imeiMd5",reportDTO.getImeiSum());
        params.put("oaidMd5",reportDTO.getOaidSum());
        params.put("idfaMd5",reportDTO.getIdfaSum());
        params.put("androidId",reportDTO.getHash_android_id());
        params.put("caid",reportDTO.getCaid());
        params.put("clientIp",reportDTO.getIp());
        params.put("userAgent",reportDTO.getUser_agent());
        params.put("time", time);
        params.put("token",token);
        params.put("reportType",1);
        params.put("packId",packId);
        params.put("channel",channel);
        String allCallbackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        params.put("callback",allCallbackUrl);

        String urlParams = UrlUtil.covertParamsOrderByAsc(params);
        return accountDTO.getDspReportUrl() + "?" + urlParams;
    }
}
